package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.NationMgr;
import ejb.dto.NationDTO;

@ManagedBean
@RequestScoped
public class NationBean {
	
	public NationBean() {
		nation = new NationDTO();
	}
	
	@EJB
	private NationMgr nationMgr; 
	
	@SuppressWarnings("unused")
	private List<NationDTO> allNations;

	private int nationId;
	private NationDTO nation;
	
	/**
	 * Delete Nation from database
	 * @param id
	 * @return redirect to page <code>manageNations</code>
	 */
	public String deleteNation(int id) {
		boolean result = nationMgr.deleteNation(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/manageNations?faces-redirect=true";
	}
	
	/**
	 * Update Nation in database
	 * @return redirect to page <code>manageNations</code>
	 */
	public String updateNation() {
		boolean result = nationMgr.updateNation(nation);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/manageNations?faces-redirect=true";
	}
	
	/**
	 * Insert Nation in database
	 * @return redirect to page <code>manageNations</code>
	 */
	public String insertNation() {
		boolean result = nationMgr.insertNation(nation);
		if(!result) {
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/manageNations?faces-redirect=true";
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<NationDTO> getAllNations() {
		return nationMgr.getAllNationsDTO();
	}

	public void setAllNations(List<NationDTO> allNations) {
		this.allNations = allNations;
	}


	public int getNationId() {
		return nationId;
	}

	public void setNationId(int nationId) {
		this.nationId = nationId;
		nation = nationMgr.getNationDTO(nationId);
		if(nation == null)
			SessionBean.notFound();
	}

	public NationDTO getNation() {
		return nation;
	}

	public void setNation(NationDTO nation) {
		this.nation = nation;
	}

	
}
